/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.worker.controller.processors;

import java.util.LinkedList;
import java.util.List;

import org.ourgrid.common.FileTransferInfo;
import org.ourgrid.common.interfaces.MessageProcessor;
import org.ourgrid.common.interfaces.WorkerClient;
import org.ourgrid.reqtrace.Req;
import org.ourgrid.worker.controller.FileTransferController;
import org.ourgrid.worker.controller.actions.GetFilesMessageHandle;
import org.ourgrid.worker.dao.GridProcessDAO;
import org.ourgrid.worker.dao.WorkerStatusDAO;
import org.ourgrid.worker.messages.WorkerControllerMessages;

import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;


public class GetFilesProcessor implements MessageProcessor<GetFilesMessageHandle> {

	@Req("REQ081")
	/**
	 * Request a set of files to be sent by the <code>Worker</code> to a
	 * client.
	 * 
	 * @param handle the handle contains the path of requested files.
	 */
	public void process(GetFilesMessageHandle handle, ServiceManager serviceManager) {
		FileTransferInfo[] files = handle.getFiles();
		List<String> filePathList = createFilePathList(files);
		
		String senderPublicKey = serviceManager.getSenderPublicKey();
		WorkerStatusDAO workerStatusDAO = serviceManager.getDAO(WorkerStatusDAO.class);

		if(!senderPublicKey.equals(workerStatusDAO.getConsumerPublicKey())) {
			serviceManager.getLog().warn(WorkerControllerMessages.getUnknownClientRequestsToRecoverFilesMessage(filePathList, senderPublicKey));
			return;
		}

		if(!workerStatusDAO.hasConsumer()) {
			serviceManager.getLog().warn(WorkerControllerMessages.getClientRequestsToRecoverFilesOnUnstartedWorkerMessage(filePathList, senderPublicKey));
			return;
		}

		if(!serviceManager.getDAO(GridProcessDAO.class).isExecutionFinish()) {
			serviceManager.getLog().warn(WorkerControllerMessages.getClientRequestsToRecoverFilesBeforeExecutionFinishMessage(filePathList, senderPublicKey));
			return;
		}

		if(workerStatusDAO.isErrorState()) {
			serviceManager.getLog().warn(WorkerControllerMessages.getClientRequestsToRecoverFilesOnWorkerWithErrorMessage(filePathList, senderPublicKey));
			return;
		}

		WorkerClient consumer = workerStatusDAO.getConsumer();
		DeploymentID consumerID = serviceManager.getStubDeploymentID(consumer);
		FileTransferController.getInstance().startTransfer(consumerID, serviceManager, files);
	}

	@Req("REQ081")
	private List<String> createFilePathList(FileTransferInfo... infos) {
		List<String> list = new LinkedList<String>(); 
		if(infos != null) {
			for (FileTransferInfo info : infos) {
				list.add(info.getFilePath());
			}
			return list;
		}
		return null;
	}

	
}
